﻿using PaperMaster.Chess.Bell;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PaperMaster.Chess
{
    /// <summary>
    /// 回合的主人，包括自己和敌人。
    /// </summary>
    public enum Turn
    {
        /// <summary>
        /// 占位符
        /// </summary>
        None,
        Player,
        Oppoent
    }
    /// <summary>
    /// 该类单纯用于控制玩家回合的开始与结束。
    /// </summary>
    public class BellController: IFightBeginCommand, IFightEndCommand
    {
        /// <summary>
        /// 获取当前回合的主人
        /// </summary>
        /// <returns></returns>
        public static Turn GetTurn()
        {
            var controller = BellViewController.GetController();
            if (controller!=null)
            {
                return BellViewController.GetController()._currentTurn;
            }
            return Turn.None;
        }
        /// <summary>
        /// 结束当前回合
        /// </summary>
        public static void EndTurn()
        {
            var controller = BellViewController.GetController();
            if (controller != null)
            {
                if (!controller._finishTurn)
                {
                    controller._finishTurn = true;
                }
            }
        }
        /// <summary>
        /// 当前谁的回合
        /// </summary>
        Turn _currentTurn;
        /// <summary>
        /// 
        /// </summary>
        bool _finishTurn = false;
        bool _enabled = false;

        public BellController()
        {
            InitData();
        }
        ~BellController()
        {
            ClearData();
        }

        public void FinishPlayerTurn()
        {
            if (_currentTurn==Turn.Player && !_finishTurn)
            {
                _finishTurn = true;
            }
        }
        public void FinishOppoentTurn()
        {
            if(_currentTurn==Turn.Oppoent && !_finishTurn)
            {
                _finishTurn = true;
            }
        }

        void InitData()
        {
            ClearData();
            _currentTurn = Turn.Player;
            _finishTurn = false;
        }
        void ClearData()
        {

        }
        #region 响应函数
        void OnFightBegin()
        {
            _enabled = true;
        }
        void OnFightEnd()
        {
            _enabled = false;
            InitData();
        }
        #endregion
        public void FrameFun()
        {
            if (_enabled)
            {
                Listen();
                Reason();
                EventSender();
            }
        }
        /// <summary>
        /// 监听用户动向
        /// </summary>
        void Listen()
        {
            ///这段代码仅仅用于测试
            if (Input.GetKeyDown(KeyCode.N))
            {
                Debug.Log("就这样吧");
                FinishPlayerTurn();
            }
        }

        /// <summary>
        /// 判断是否符合结束回合条件，若符合则将finishTurn设为True
        /// </summary>
        void Reason()
        {
        }

        /// <summary>
        /// 当确定符合某些条件时，触发对应事件。
        /// </summary>
        void EventSender()
        {
            if (_finishTurn)
            {
                _finishTurn = false;
                if (_currentTurn == Turn.Player)
                {
                    _currentTurn = Turn.Oppoent;
                    PlayerTurnEndEvent.Trigger();
                }
                else
                {
                    _currentTurn = Turn.Player;
                    PlayerTurnBeginEvent.Trigger();
                }
            }
        }

        public void FightEndExecute()
        {
            OnFightEnd();
        }

        public void FightBeginExecute()
        {
            OnFightBegin();
        }
    }
}
